function formatterClassGrade(value, row, index) {
    return value ? value.name : "";
}
function formatterTeacher(value, row, index) {
    return value ? value.realName : "";
}
function formatterCoordinator(value, row, index) {
    return value ? value.realName : "";
}
function formatterClassroom(value, row, index) {
    return value ? value.name : "";
}
function formatterStatus(value, row, index) {
    //获取课程时间
    var date=row.subDate;
    date = date.substring(0,19);
    date = date.replace(/-/g,'/');
    var scheduleDate = new Date(date).getTime();
    //获取现在的时间
    var newDate=new Date();
    if(newDate.getTime()>scheduleDate){
        return "<span style='color:green'>结课</span>";
    }else{
        return "<span style='color:red'>未上</span>";
    }
}
$(function () {
    //抽取变量
    var schedule_datagrid = $("#schedule_datagrid");
    var schedule_import = $("#schedule_import");
    schedule_import.dialog({
        title: '文件上传',
        width: 300,
        height: 130,
        closed: true
    });
    var cmdObj = {
        upload:function () {
            var files = $("#schedule_xls").filebox("getValue");
            if (!files) {
                $.messager.alert("温馨提示", "请选择课程表进行上传", "info");
            }else{
                $("#import_form").form("submit", {
                    url: "/schedule/importXls",
                    success: function (data) {
                        var data = $.parseJSON(data);
                        if (data.success) {
                            $.messager.alert("温馨提示", "导入成功!", "info", function () {
                                $("#schedule_import").dialog("close");
                                schedule_datagrid.datagrid("reload");
                            });
                        } else {
                            $.messager.alert("温馨提示", "导入失败!", "error");
                        }
                    }
                });
            }
        },
        importSchedule:function () {
            schedule_import.dialog("open");
        },
        //模板下载
        download:function () {
            window.location.href="/schedule/download";
        },
        undo:function(){
            schedule_import.dialog("close");
        },
        //文件导出
        exportSchedule:function(){
            location.href="/schedule/export";
        },
        //课表的增删改和导入功能
        addSchedule:function () {
            //清除表单数据
            $("#schedule_add_form").form("clear");
            //设置标题
            $("#schedule_add_dialog").dialog("setTitle", "添加课程");
            //打开弹窗
            $("#schedule_add_dialog").dialog("open");
        },
        reloadSchedule: function () {
            //刷新时清空原内容
            $("#btn_schedule").combobox("clear");
            $("#btn_classroom").combobox("clear");
            $("#btn_teacher").combobox("clear");
            $("#btn_beginDate").datebox("clear");
            $("#btn_endDate").datebox("clear");
            $.get("/schedule/list", function (data) {
                schedule_datagrid.datagrid("loadData", data);
            });
        },
        searchForSchedule: function () {
            //获取到输入值
             var classId = $("#btn_classgrade").val();
            var roomId = $("#btn_classroom").val();
            var teacherId = $("#btn_teacher").val();
            var beginDate = $("#btn_beginDate").val();
            var endDate = $("#btn_endDate").val();

            schedule_datagrid.datagrid("load", {
               classId: classId,
                 roomId: roomId,
                teacherId: teacherId,
                beginDate: beginDate,
                endDate: endDate
            });
        },
       //编辑课程
        editSchedule:function () {
            //获取选中行
            var selectedRow=$("#schedule_datagrid").datagrid("getSelected")
            if(!selectedRow){
                $.messager.alert("提示","请选中一行数据进行操作","error")
                return;
            }

            //对关联对象的回显
            if(selectedRow.classgrade){
                selectedRow['classgrade.id']=selectedRow.classgrade.id;
            }
            if(selectedRow.employee){
                selectedRow['employee.id']=selectedRow.employee.id;
            }
            if(selectedRow.classroom){
                selectedRow['classroom.id']=selectedRow.classroom.id;
            }
            //读取选中行的数据进行回填
            $("#schedule_add_form").form("load",selectedRow);
            //设置标题
            $("#schedule_add_dialog").dialog("setTitle", "编辑班级");
            //打开弹窗
            $("#schedule_add_dialog").dialog("open");

        },
        removeSchedule:function () {
          //取消课程
            //获取选中行
            var selectedRow=$("#schedule_datagrid").datagrid("getSelected")
            if(!selectedRow){
                $.messager.alert("提示","请选中一行数据进行操作","error")
                return;
            }else{
                $.messager.confirm("提示","您确定要取消该课程吗?想想那些好学的学生吧!",function (yes) {
                    if(yes){
                        $.messager.alert("提示","就是不给你删","error");
                    }else{
                        //获取选中行后
                        $.post("/schedule/delete",{id:selectedRow.id},function (data) {
                            if(data.success){
                                $.messager.alert("提示","取消成功","info",function () {
                                    cmdObj.reloadSchedule();
                                })
                            }else{
                                $.messager.alert("提示","该课程不能被删除,学生一定要上","error")
                            }
                        })
                    }
                })
            }


            
        },
        save: function () {
            var url="/schedule/save";
            //获取选中行id
            var scheduleId=$("#schedule_id").val();
            if(scheduleId){
                url="/schedule/update";
            }
            $("#schedule_add_form").form('submit',{
                url: url,
                success: function (data) {
                    data=$.parseJSON(data)
                    if(data.success){

                        $.messager.alert("提示","保存成功","info",function () {
                            //关闭窗口
                            cmdObj.cancel();
                            //刷新页面
                            cmdObj.reloadSchedule();
                        })
                    }else{
                        $.messager.alert("提示",data.msg,"error");
                    }
                }
            });

        }, //关闭dialog
        cancel:function () {
            $("#schedule_add_dialog").dialog("close");
        },
        reloadSchedule:function () {
            $("#schedule_datagrid").datagrid("reload")
        }
    };
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    });
    //弹窗渲染
    $("#schedule_add_dialog").dialog({
        title: "添加课程",
        buttons: '#schedule_add_linkbuttons',
        closed: true

    })
    //初始化datagrid
    schedule_datagrid.datagrid({
        fit: true,
        fitColumns: true,
        striped: true,
        url: "/schedule/list",
        pagination: true,
        singleSelect: true,
        rownumbers: true,
        toolbar: "#schedule_toolbar",
        columns:[[
            {field:'subDate',title:'课程日期',width:100},
            {field:'subject',title:'课程名称',width:100},
            {field:'classgrade',title:'班级',width:100,formatter: formatterClassGrade},
            {field:'teacher',title:'任课教师',width:100,formatter: formatterTeacher},
            {field:'coordinator',title:'班主任',width:100,formatter: formatterCoordinator},
            {field:'classroom',title:'教室',width:100,formatter: formatterClassroom},
            {field:'status',title:'状态',width:100,formatter: formatterStatus},
            {field:'remark',title:'备注',width:100}
        ]]
    });
    //用户选择日期时触发
    $('#calendar').calendar({
        onSelect: function(date){
            var data = date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate();
            $("#schedule_datagrid").datagrid("load", {
                beginDate: data,
                endDate: data
            });
        }
    });
});